package com.spsy.pay.controller;


import com.alibaba.fastjson.JSON;
import com.spsy.common.core.utils.R;
import com.spsy.common.vo.OrderVo1;
import com.spsy.order.api.OrderApi;
import com.spsy.pay.domain.PayData;
import com.spsy.pay.utils.WxPayUtils;
import com.spsy.pay.domain.TPaymentOrder;
import com.spsy.pay.service.ITPaymentOrderService;
import com.spsy.pay.utils.ZhifubaoUtill;
import com.spsy.pay.vo.OrderPayEnum;
import com.spsy.pay.vo.OrderVo;
import com.spsy.pay.vo.WeixinNotifyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Kyrie Irving
 * @Date: 2025/9/16 18:50
 * @Description:
 **/
@RestController
@RequestMapping("/t_payment_order")
@Slf4j
public class TestPayController {
    @Autowired
    private ITPaymentOrderService itPaymentOrderService;


    @Autowired
    private OrderApi orderApi;

    @PostMapping("/pay")
    public R pay(@RequestBody OrderVo orderVo) throws Exception {
        TPaymentOrder paymentOrder = TPaymentOrder.builder()
                .merchantOrderNo(orderVo.getOrderId())
                .userId((long) 1)
                .payMay("微信")
                .merchantId((long)1)
                .payAmount(new BigDecimal(0.01))
                .orderStatus(OrderPayEnum.NOT_PAY.getStatusValue().longValue())
                .payTime(new Date())
                .build();
        String s = WxPayUtils.wxPay(orderVo.getOrderId());
        int save = itPaymentOrderService.insertTPaymentOrder(paymentOrder);
        System.out.println(R.OK(s));
        return save>0 ? R.OK(s) : R.ERROR(500,"支付失败");
    }

    @PostMapping("/payOrder")
    public R payOrder(@RequestBody OrderVo1 orderVo){
        log.info("{}",JSON.toJSONString(orderVo));
        orderApi.updateOrderStatus(orderVo);
        log.info("asddsa{}", JSON.toJSONString(orderVo));
        return R.OK();
    }
    @PostMapping("/payZFB")
    public R payZFB(@RequestBody PayData payData) throws Exception {
        TPaymentOrder paymentOrder = TPaymentOrder.builder()
                .merchantOrderNo(payData.getCode())
                .userId((long) 1)
                .payMay("支付宝")
                .merchantId((long)1)
                .payAmount(new BigDecimal(0.01))
                .orderStatus(OrderPayEnum.NOT_PAY.getStatusValue().longValue())
                .payTime(new Date())
                .build();
        String s = WxPayUtils.wxPay(payData.getCode());
        int save = itPaymentOrderService.insertTPaymentOrder(paymentOrder);
        Map pay = new ZhifubaoUtill().pay(payData);
        log.info(pay.toString());
        return save>0 ? R.OK(pay) : R.ERROR(500,"支付失败");
    }


    @PostMapping("/updatePayOrderStatus")
    public R updatePayOrderStatus(@RequestBody WeixinNotifyVo weixinNotifyVo) throws Exception {
        log.info(weixinNotifyVo.toString());
        if (weixinNotifyVo.getOrderCode().contains("success")){
            int i = itPaymentOrderService.updatePayOrderByOrderId(TPaymentOrder.builder()
                    .merchantOrderNo(weixinNotifyVo.getOrderNo()).orderStatus(OrderPayEnum.success.getStatusValue().longValue()).build());
        return R.OK();
        }else {
            return R.ERROR(500,"支付失败");
        }

    }
    private static final Object lock = new Object();
    private static int n = 0;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for (int i = 0; i <= 10000; i++) {
            threadPool.submit(() -> {
                synchronized (lock) { // 获取锁
                    n = n + 1;
                } // 释放锁
            });
        }
        threadPool.shutdown();
        threadPool.awaitTermination(1, TimeUnit.MINUTES);
        System.out.println(n); // 现在输出一定是 10000
    }

}
